package com.osce.face.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.face.FaceWhiteList;
import com.osce.common.utils.DateUtils;
import com.osce.face.mapper.FaceWhiteListMapper;
import com.osce.interfaces.face.IFaceWhiteListService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 人脸白名单Service业务层处理
 *
 * @author zzx
 * @since 2023-11-21
 */
@Service
public class FaceWhiteListServiceImpl extends ServiceImpl<FaceWhiteListMapper, FaceWhiteList> implements IFaceWhiteListService {


    /**
     * 查询人脸白名单
     *
     * @param id 人脸白名单主键
     * @return 人脸白名单
     */
    @Override
    public FaceWhiteList selectFaceWhiteListById(Long id) {
        return baseMapper.selectFaceWhiteListById(id);
    }

    /**
     * 查询人脸白名单列表
     *
     * @param faceWhiteList 人脸白名单
     * @return 人脸白名单
     */
    @Override
    public List<FaceWhiteList> selectFaceWhiteListList(FaceWhiteList faceWhiteList) {
        return baseMapper.selectFaceWhiteListList(faceWhiteList);
    }

    /**
     * 新增人脸白名单
     *
     * @param faceWhiteList 人脸白名单
     * @return 结果
     */
    @Override
    public int insertFaceWhiteList(FaceWhiteList faceWhiteList) {
        faceWhiteList.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFaceWhiteList(faceWhiteList);
    }

    @Override
    public int insertMoreFaceWhiteList(FaceWhiteList faceWhiteList) {
        List<Long> userList = faceWhiteList.getUserList();
        List<Long> placeList = faceWhiteList.getPlaceList();
        AtomicInteger rows = new AtomicInteger(0);
        placeList.forEach(placeId -> {
            userList.forEach(userId -> {
                FaceWhiteList white = baseMapper.selectFaceWhiteListByUserIdAndPlaceId(userId, placeId);
                if (white == null || !white.getForever().equals(1)) {//如果不存在永久的白名单则添加，不论时间区间
                    FaceWhiteList copyBean = BeanUtil.copyProperties(faceWhiteList, FaceWhiteList.class);
                    copyBean.setPlaceId(placeId);
                    copyBean.setUserId(userId);
                    copyBean.setCreateTime(DateUtils.getNowDate());
                    rows.addAndGet(insertFaceWhiteList(copyBean));
                }
            });
        });
        return rows.get();
    }

    /**
     * 修改人脸白名单
     *
     * @param faceWhiteList 人脸白名单
     * @return 结果
     */
    @Override
    public int updateFaceWhiteList(FaceWhiteList faceWhiteList) {
        return baseMapper.updateFaceWhiteList(faceWhiteList);
    }

    /**
     * 批量删除人脸白名单
     *
     * @param ids 需要删除的人脸白名单主键
     * @return 结果
     */
    @Override
    public int deleteFaceWhiteListByIds(Long[] ids) {
        return baseMapper.deleteFaceWhiteListByIds(ids);
    }

    /**
     * 删除人脸白名单信息
     *
     * @param id 人脸白名单主键
     * @return 结果
     */
    @Override
    public int deleteFaceWhiteListById(Long id) {
        return baseMapper.deleteFaceWhiteListById(id);
    }
}
